/**
 * Timing.h
 *
 * @author Bematech S.A.
 */

#include "bilbao/util/Timing.h"

namespace bilbao
{
	namespace util
	{
		//--------------------------------------------------------------------------------
		// Class Timing
		//--------------------------------------------------------------------------------
		// Métodos Públicos
		//--------------------------------------------------------------------------------
		Timing::Timing( void )
		{
			//Do nothing
		}
		//--------------------------------------------------------------------------------
		Timing::~Timing( void )
		{
			//Do nothing
		}
		//--------------------------------------------------------------------------------
		void Timing::getTimeStamp( String& result )
		{
#ifdef BILBAO_CPLUPLUS
			getTimeStampCpp(result);
#else
			char ret[14] = {0};
			getTimeStampC( ret );

			result += ret;
#endif
		}
		//--------------------------------------------------------------------------------
		void Timing::getShortTimeStamp( String& result )
		{
#ifdef BILBAO_CPLUPLUS
			getShortTimeStampCpp( result );
#else
			char ret[6] = {0};
			getShortTimeStampC( ret );

			result += ret;
#endif
		}
		//--------------------------------------------------------------------------------
		// Métodos Privados
		//--------------------------------------------------------------------------------
#ifdef BILBAO_CPLUPLUS
		void Timing::getTimeStampCpp( String& result )
		{
			// gets the local time
			time_t now;
			time(&now);

			// gets the date/time
			tm *ltime = localtime(&now);
			StringStream stream;

			// print the date part?
			//aaaammddhhmmss
			stream << std::setw(4) << std::setfill('0') << (ltime->tm_year + 1900);
			stream << std::setw(2) << std::setfill('0') << (ltime->tm_mon + 1);
			stream << std::setw(2) << std::setfill('0') << (ltime->tm_mday);
			stream << std::setw(2) << std::setfill('0') << (ltime->tm_hour);
			stream << std::setw(2) << std::setfill('0') << (ltime->tm_min);
			stream << std::setw(2) << std::setfill('0') << (ltime->tm_sec);

			result += stream.str();
		}
		//--------------------------------------------------------------------------------
		void Timing::getShortTimeStampCpp( String& result )
		{
			// gets the local time
			time_t now;
			time(&now);

			// gets the date/time
			tm *ltime = localtime(&now);
			StringStream stream;

			// print the date part?
			//aaaamm
			stream << std::setw(4) << std::setfill('0') << (ltime->tm_year + 1900);
			stream << std::setw(2) << std::setfill('0') << (ltime->tm_mon + 1);

			result += stream.str();
		}
#else
		void Timing::getTimeStampC( char result[14] )
		{
			// gets the local time
			time_t now;
			time( &now );

			// gets the date/time
			tm *ltime = localtime( &now );

			// print the date part?
			//aaaammddhhmmss
			sprintf( result, "%04d%02d%02d%02d%02d%02d",
					(ltime->tm_year + 1900),
					(ltime->tm_mon + 1),
					(ltime->tm_mday),
					(ltime->tm_hour),
					(ltime->tm_min),
					(ltime->tm_sec));
		}
		//--------------------------------------------------------------------------------
		void Timing::getShortTimeStampC( char result[6] )
		{
			// gets the local time
			time_t now;
			time( &now );

			// gets the date/time
			tm *ltime = localtime( &now );

			// print the date part?
			//aaaamm
			sprintf( result, "%04d%02d",
					(ltime->tm_year + 1900),
					(ltime->tm_mon + 1));
		}
#endif
	}
}
